package com.mcj.demo;

import com.mcj.entity.Author;
import com.mcj.entity.Book;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntPredicate;
import java.util.function.Predicate;

public class StreamDemo02 {
	
	public static void main(String[] args) {
		testNegate();
	}
	
	// Predicate negate() 组合两个判断条件 取反
	private static void testNegate() {
		// 打印作家中年龄不大于17的作家姓名
		List<Author> authors = getAuthors();
		authors.stream()
				.filter(new Predicate<Author>() {
					@Override
					public boolean test(Author author) {
						return author.getAge() > 17;
					}
				}.negate())
				.forEach(author -> System.out.println(author.getAge() + "-" + author.getName()));
	}
	
	// Predicate or() 组合两个判断条件 或
	private static void testOr() {
		// 打印作家中年龄大于17或姓名长度小于2的作家名字
		List<Author> authors = getAuthors();
		authors.stream()
				.filter(new Predicate<Author>() {
					@Override
					public boolean test(Author author) {
						return author.getAge() > 17;
					}
				}.or(new Predicate<Author>() {
					@Override
					public boolean test(Author author) {
						return author.getName().length() < 2;
					}
				}))
				.distinct()
				.forEach(author -> System.out.println(author.getAge() + "-" + author.getName()));
	}
	
	// 自定义方法 and
	private static void test02() {
		printNum(value -> (value % 2) == 0, value -> value > 4);
	}
	
	// Predicate and() 组合两个判断条件 与
	private static void testAnd() {
		// 打印作家中年龄大于17并且姓名长度大于1的作家
		List<Author> authors = getAuthors();
		authors.stream()
				.filter(new Predicate<Author>() {
					@Override
					public boolean test(Author author) {
						return author.getAge() > 17;
					}
				}.and(new Predicate<Author>() {
					@Override
					public boolean test(Author author) {
						return author.getName().length() > 1;
					}
				}))
				.forEach(author -> System.out.println(author.getAge() + "-" + author.getName()));
	}
	
	private static void printNum(IntPredicate predicate1, IntPredicate predicate2) {
		int[] arr = {1,2,3,4,5,6,7,8,9,10};
		for (int i : arr) {
			if (predicate1.and(predicate2).test(i)) {
				System.out.println(i);
			}
		}
	}
	
	private static List<Author> getAuthors() {
		//数据初始化
		Author author1 = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
		Author author2 = new Author(2L,"亚索",15,"狂风也追逐不上他的思考速度",null);
		Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
		Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
		
		//书籍列表
		List<Book> books1 = new ArrayList<>();
		List<Book> books2 = new ArrayList<>();
		List<Book> books3 = new ArrayList<>();
		
		books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
		books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));
		
		books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
		books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
		books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));
		
		books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么的宽容"));
		books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
		books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
		
		author1.setBooks(books1);
		author2.setBooks(books2);
		author3.setBooks(books3);
		author4.setBooks(books3);
		
		return new ArrayList<>(Arrays.asList(author1,author2,author3,author4));
	}
}
